package com.wufun.overseas.tools.socket;

import android.util.Log;

import com.wufun.baselibrary.utils.AppInfoUtil;
import com.wufun.overseas.tools.socket.entity.BaseSocketEvent;
import com.wufun.overseas.tools.socket.entity.SocketFileRequestBean;
import com.wufun.overseas.tools.socket.entity.SocketTextRequestBean;

/**
 * Author: hest
 * Date:  2016/5/27
 * Socket 命令管理
 */
public class SocketCommendManager {
    private static final String TAG = SocketCommendManager.class.getSimpleName();
    private static final int DEFAULT_PER_TASK_SEND_SIZE = 5;
    RequestQueneManager requestManager = RequestQueneManager.getInstance();

    private static final class Holder {
        public static final SocketCommendManager sINSTANCE = new SocketCommendManager();
    }

    public static SocketCommendManager getInstance() {
        return Holder.sINSTANCE;
    }

    //发送设备型号
    public void sendDeviceModel() {
        if (requestManager == null) {
            return;
        }
        String content = BaseSocketEvent.SOCKET_DEVICE_MODLE_HEAD + AppInfoUtil.getDeviceModel();
        SocketTextRequestBean bean = new SocketTextRequestBean(content);
        requestManager.push(bean);
    }

//    //发送DownloadTask列表信息
//    public void sendTaskList(List<DownloadTask> taskList) {
//        if (taskList != null) {
//            List<List<DownloadTask>> result = createList(taskList, DEFAULT_PER_TASK_SEND_SIZE);
//            int taskSize = taskList.size();
//            for (List<DownloadTask> subArr : result) {
//                String taskStr = JsonMapper.getInstance().toJson(subArr);
//                Log.d(TAG, "Task列表: " + taskStr);
//                taskStr = BaseSocketEvent.SOCKET_TASK_LIST_HEAD + taskSize + ":" + taskStr;
//                SocketTextRequestBean bean = new SocketTextRequestBean(taskStr);
//                requestManager.push(bean);
//            }
//        }
//    }
//
//
//    //ListView 拆成等分
//    public static List<List<DownloadTask>> createList(List<DownloadTask> targe, int size) {
//        List<List<DownloadTask>> listArr = new ArrayList<List<DownloadTask>>();
//        //获取被拆分的数组个数
//        int arrSize = targe.size() % size == 0 ? targe.size() / size : targe.size() / size + 1;
//        for (int i = 0; i < arrSize; i++) {
//            List<DownloadTask> sub = new ArrayList<DownloadTask>();
//            //把指定索引数据放入到list中
//            for (int j = i * size; j <= size * (i + 1) - 1; j++) {
//                if (j <= targe.size() - 1) {
//                    sub.add(targe.get(j));
//                }
//            }
//            listArr.add(sub);
//        }
//        return listArr;
//    }

    //正接收任务列表信息
    public void acceptTaskList() {
        if (requestManager == null) {
            return;
        }
        SocketTextRequestBean bean = new SocketTextRequestBean(BaseSocketEvent.SOCKET_TASK_LIST_ACCEPTION);
        requestManager.push(bean);
    }

    //当downloadTask列表信息接收完发送回调信息
    public void sendDownloadTaskListReceiverResult() {
        if (requestManager == null) {
            return;
        }
        SocketTextRequestBean bean = new SocketTextRequestBean(BaseSocketEvent.SOCKET_TASK_LIST_OK);
        requestManager.push(bean);
    }

    //发送单个DownloadTask信息
    public void sendTask(String idAndCode) {
        if (idAndCode != null) {
            String taskStr = BaseSocketEvent.SOCKET_SINGLE_TASK_HEAD + idAndCode;
            SocketTextRequestBean bean = new SocketTextRequestBean(taskStr);
            requestManager.push(bean);
        }
    }

    //发送downloadTask信息接收完回调信息需要发送文件
    public void sendDownloadTaskYes() {
        if (requestManager == null) {
            return;
        }
        SocketTextRequestBean bean = new SocketTextRequestBean(BaseSocketEvent.SOCKET_SINGLE_TASK_YES);
        requestManager.push(bean);
    }

    //发送downloadTask信息接收完回调信息不需要发送文件
    public void sendDownloadTaskNo() {
        if (requestManager == null) {
            return;
        }
        SocketTextRequestBean bean = new SocketTextRequestBean(BaseSocketEvent.SOCKET_SINGLE_TASK_NO);
        requestManager.push(bean);
    }

    //不需要发送游戏的发送方更新完界面后
    public void senderUpdateUI() {
        if (requestManager == null) {
            return;
        }
        SocketTextRequestBean bean = new SocketTextRequestBean(BaseSocketEvent.SOCKET_NO_SEND_UPDATE_UI_OK);
        requestManager.push(bean);
    }

    //发送文件
    public void sendFile(SocketFileRequestBean bean) {
        if (bean != null) {
            requestManager.push(bean);
        }
    }

    //所有文件发送完成
    public void senderFileFinish() {
        if (requestManager == null) {
            return;
        }
        SocketTextRequestBean bean = new SocketTextRequestBean(BaseSocketEvent.SOCKET_SERVICE_SEND_FINISH);
        requestManager.push(bean);
    }

    //文件文件压缩中
    public void senderFileZip() {
        if (requestManager == null) {
            return;
        }
        SocketTextRequestBean bean = new SocketTextRequestBean(BaseSocketEvent.SOCKET_SEND_ZIP);
        requestManager.push(bean);
    }


    //发送心跳检测数据
    public void sendHeartMsg() {
        if (requestManager == null) {
            return;
        }
        SocketTextRequestBean hearBeatBean = new SocketTextRequestBean(BaseSocketEvent.SOCKET_HEART_BEAT);
        requestManager.push(hearBeatBean);
    }

    //发送方SdCard 内存不足信息
    public void sendSdCardSizeLimite(String id) {
        if (id != null) {
            String taskStr = BaseSocketEvent.SOCKET_SEND_SDCARD_SIZE_LIMITE_HEAD + id;
            SocketTextRequestBean bean = new SocketTextRequestBean(taskStr);
            requestManager.push(bean);
        }
    }


    //接收方SdCard 内存不足信息
    public void receiverSdCardSizeLimite(String id) {
        if (id != null) {
            String taskStr = BaseSocketEvent.SOCKET_RECEIVER_SDCARD_SIZE_LIMITE_HEAD + id;
            SocketTextRequestBean bean = new SocketTextRequestBean(taskStr);
            requestManager.push(bean);
        }
    }

    public void clearAllCommend() {
        requestManager.clearAll();
    }

}
